package com.whl.sm.controller.expense;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.model.expense.ExpenseType;
import com.whl.sm.model.system.SysDict;
import com.whl.sm.service.expense.ExpenseTypeService;
import com.whl.sm.service.system.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-18
 */
@RestController
@RequestMapping("/expense-type")
public class ExpenseTypeController extends BaseController {

    @Autowired
    private ExpenseTypeService expenseTypeService;

    @Autowired
    private SysDictService sysDictService;

    @RequestMapping("/list")
    public Result getExpenseList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("expense_name");
        setHttpRequestQueryWrapper(queryWrapper,"expense_type","pay_remark");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> expensePage = expenseTypeService.page(getPage(),queryWrapper);
        expensePage.getRecords().stream().forEach(obj->{
            ExpenseType expenseType = (ExpenseType) obj;
            SysDict expenseDict =  sysDictService.getById(expenseType.getExpenseType());
            expenseType.setExpenseTypeName(expenseDict.getDictValue());
        });
        return Result.ok(expensePage);
    }

    @RequestMapping("/info/{id}")
    public Result expenseInfo(@PathVariable("id") String id){
        ExpenseType expenseType = expenseTypeService.getById(id);
        return Result.ok(expenseType);
    }


    @RequestMapping("/update")
    public Result updateUser(@RequestBody ExpenseType expenseType){
        return expenseTypeService.update(expenseType);
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            expenseTypeService.update(new UpdateWrapper<ExpenseType>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

    @RequestMapping("/expenseTypeByKey")
    public Result getSelectExpenseType(@RequestParam String dictKey){
        List<SelectDto> result = new ArrayList<>();
        SysDict expenseType = sysDictService.getOne(new QueryWrapper<SysDict>()
                .eq("dict_value",dictKey)
        );
        if (expenseType != null){
            List<ExpenseType> expenseTypeList = expenseTypeService.list(new QueryWrapper<ExpenseType>()
                    .eq("expense_type",expenseType.getId())
            );
            result= expenseTypeList.stream().map(obj->{
                return new SelectDto(obj.getId(),obj.getExpenseName(),null,false);
            }).collect(Collectors.toList());
        }
        return Result.ok(result);
    }



}
